---
title: "joint-figures"
output: html_document
date: "2025-10-01"
---
<!-- Set working directory -->

```{r setup, include=FALSE, cache=FALSE}
rm(list=ls())
require("knitr")

knitr::opts_knit$set(root.dir = "C:/Users/qs932404/Dropbox/USmidterms/R&R EJPR/Final submission/replication-files")


# set your own working directory by adjusting the below line - to where you saved the replication files:
#eg knitr::opts_knit$set(root.dir = "~/Downloads/replication-files")



```


<!--Load all the required libraries in-->

```{r libraries, include=FALSE, cache = FALSE}

library(tidyr)

library(dplyr)

library(stringr)

```


```{r theme}


theme_report_forcovar_wlegend <- function(base_size = 14) {
  theme_bw(base_size = base_size) %+replace%
    theme(
      # L'ensemble de la figure
      plot.title = element_text(size = rel(1), face = "bold", margin = margin(0,0,5,0), hjust = 0),
      # Zone où se situe le graphique
      panel.grid.minor = element_blank(),
       panel.grid.major = element_blank(),
      panel.border = element_blank(),
      # Les axes
      axis.title = element_text(size = rel(0.85), face = "bold"),
      axis.text = element_text(size = rel(0.70), face = "bold"),
      axis.line = element_line(color = "black"),
      # La légende
      #legend.position = "none",
      legend.title = element_text(size = rel(0.85), face = "bold"),
      legend.text = element_text(size = rel(0.70), face = "bold"),
      legend.key = element_rect(fill = "transparent", colour = NA),
      legend.key.size = unit(1.5, "lines"),
      legend.background = element_rect(fill = "transparent", colour = NA),
      # Les étiquettes dans le cas d'un facetting
      strip.background = element_rect(fill = "#17252D", color = "#17252D"),
      strip.text = element_text(size = rel(0.85), face = "bold", color = "white", margin = margin(5,0,5,0))
    )
}



```

Note: Fig 1 are images in text, not generated from experiment data.


# Figure 2, Main text

```{r fig2}

s1 <- readRDS("data_for_figures/s1_f4_main_opentext.rds") %>%
  mutate(study = "Study 1")

s2 <- readRDS("data_for_figures/s2_f4_main_opentext.rds")%>%
  mutate(study = "Study 2")


comb <- s1 %>% rbind(., s2)

library(ggplot2)
comb %>%
  mutate(treatment = factor(treatment, levels=c("Anger", "Worry", "Pooled", "Placebo")))%>%
  filter(treatment!="Placebo")%>%
  filter(!str_detect(sentiment, "positive")) %>%
  ggplot(data=., aes(x=sentiment, y=Beta_Estimate, color=treatment, shape=treatment)) +
  geom_point(position = position_dodge(width = 0.9))+
  geom_linerange(aes(ymax=CI_95_UL, ymin=CI_95_LL), position = position_dodge(width = 0.9))+
  geom_hline(yintercept=0)+
  theme_report_forcovar_wlegend()+
  coord_flip() +
  ylab("Marginal effect of electoral loss treatments (vs placebo), 95% CI, standardised*")+
  xlab(" \n") +
  labs(color="Treatment", shape="Treatment", caption="*coefficients are represented in standard deviations of the outcome")+
  ggtitle("Marginal effect of treatments on sentiment in the open text responses") +
   scale_color_manual(values=c('#810f7c','#43a2ca', '#e66101' )) +
    scale_x_discrete(limits=rev)+
  facet_grid(~study)



ggsave(file= "figures/f4_manip_opentext.png",width =25, height = 17, units = "cm", dpi=320)
```



# Figure 3, main text - anger

```{r fig3}

library(ggplot2)
s2 <- readRDS("data_for_figures/s2_main_emobin_angry.rds")%>%
  mutate(study = "Study 2")
s1 <- readRDS("data_for_figures/s1_main_emobin_angry.rds") %>%
  mutate(study = "Study 1")



comb <- s1 %>% rbind(., s2)

comb <- comb %>% mutate(partyid = case_when(study=="Study 1" ~ w1_partyid,
                                            study=="Study 2" ~ "Democrats"),
                        treatment = case_when(study=="Study 1" ~w2_treatment_cat,
                                            study=="Study 2" ~ treatment))

ggplot(comb, aes(x=partyid, y=estimate, fill=treatment, shape=treatment)) +
  theme_report_forcovar_wlegend() +
  geom_bar(stat="identity", position = position_dodge(width = 0.9), alpha=0.65, width=0.7) +
  geom_point(size = 1.5, position = position_dodge(width = 0.9)) +
  geom_linerange( aes(ymin=conf.low, ymax=conf.high), size = 0.5,colour= "grey50" ,position = position_dodge(width = 0.9))  +
    geom_linerange( aes(ymin=conf.low.83, ymax=conf.high.83), size = 1, position = position_dodge(width = 0.9)) +
  theme(
plot.title = element_text(hjust = 0.5, size = 12, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_blank(),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 12),
strip.text.x = element_text(size = 12),
) +
    scale_fill_manual(values=c('#636363','#810f7c','#43a2ca', '#636363','#810f7c','#43a2ca'))+
    labs(x = NULL, y = "% who say they are very or extremely angry", title= "Anger at election outcome", fill="Treatment", shape="Treatment", caption = "95% CI (grey)\n83% CI (black)")+ 
  facet_grid(~study, scales = "free_x")+
    scale_y_continuous(labels = scales::percent, limits = c(0, 0.7))


ggsave("figures/f6c_anger_binary.png",width =24, height = 15, units = "cm", dpi=320)



```

# Figure 4, main text: worry

```{r fig4}


library(ggplot2)
s2 <- readRDS("data_for_figures/s2_main_emobin_worried.rds")%>%
  mutate(study = "Study 2")
s1 <- readRDS("data_for_figures/s1_main_emobin_worried.rds") %>%
  mutate(study = "Study 1")



comb <- s1 %>% rbind(., s2)

comb <- comb %>% mutate(partyid = case_when(study=="Study 1" ~ w1_partyid,
                                            study=="Study 2" ~ "Democrats"),
                        treatment = case_when(study=="Study 1" ~w2_treatment_cat,
                                            study=="Study 2" ~ treatment))

ggplot(comb, aes(x=partyid, y=estimate, fill=treatment, shape=treatment)) +
  theme_report_forcovar_wlegend() +
  geom_bar(stat="identity", position = position_dodge(width = 0.9), alpha=0.65, width=0.7) +
  geom_point(size = 1.5, position = position_dodge(width = 0.9)) +
  geom_linerange( aes(ymin=conf.low, ymax=conf.high), size = 0.5,colour= "grey50" ,position = position_dodge(width = 0.9))  +
    geom_linerange( aes(ymin=conf.low.83, ymax=conf.high.83), size = 1, position = position_dodge(width = 0.9)) +
  theme(
plot.title = element_text(hjust = 0.5, size = 12, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_blank(),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 12),
strip.text.x = element_text(size = 12),
) +
    scale_fill_manual(values=c('#636363','#810f7c','#43a2ca', '#636363','#810f7c','#43a2ca'))+
    labs(x = NULL, y = "% who say they are very or extremely worried", title= "Worry at election outcome", fill="Treatment", shape="Treatment", caption = "95% CI (grey)\n83% CI (black)")+ 
  facet_grid(~study, scales = "free_x")+
    scale_y_continuous(labels = scales::percent, limits = c(0, 0.9))


ggsave("figures/f6c_worry_binary.png",width =24, height = 15, units = "cm", dpi=320)


```



# Fig 5, main text

```{r fig5}


s1 <- readRDS("data_for_figures/s1_f6b_main_emofactors.rds") %>%
  mutate(study = "Study 1")

s2 <- readRDS("data_for_figures/s2_f6b_main_emofactors.rds") %>%
  mutate(study = "Study 2")


comb <- s1 %>% rbind(., s2)

comb <- comb %>% mutate(dv= case_when(str_detect(dv, "disgust") ~ "Disgust",
                                      str_detect(dv, "avers") ~ "Anger",
                                      str_detect(dv, "enthusiasm") ~ "Enthusiasm",
                                      str_detect(dv, "anxiety") ~ "Anxiety"),
  emofac=factor(dv, levels = c("Anxiety", "Anger", "Disgust", "Enthusiasm")))

comb %>%
  mutate(treatment = factor(treatment, levels=c("Anger", "Worry", "Pooled", "Placebo")))%>%
  filter(treatment!="Placebo")%>%
  ggplot(data=., aes(x=emofac, y=Beta_Estimate, color=treatment, shape=treatment)) +
  geom_point(position = position_dodge(width = 0.9))+
  geom_linerange(aes(ymax=CI_95_UL, ymin=CI_95_LL), position = position_dodge(width = 0.9))+
  geom_hline(yintercept=0)+
  theme_report_forcovar_wlegend()+
  coord_flip() +
  ylab("Marginal effect of electoral loss treatments (vs placebo), 95% CI")+
  xlab("") +
  labs(color="Treatment", shape="Treatment")+
  ggtitle("Marginal effect of electoral loss treatments on feelings about the election") +
   scale_color_manual(values=c('#810f7c','#43a2ca', '#e66101' )) +
    scale_x_discrete(limits=rev)+
  facet_grid(~study)



ggsave("figures/f6b_emo_factors.png",width =24, height = 15, units = "cm", dpi=320)

```




# Figure 6, main text, political violence


```{r fig6}


s2<- readRDS("data_for_figures/s2_f8_main_polviol.rds") %>%
  mutate(study = "Study 2")
s1<- readRDS("data_for_figures/s1_f8_main_polviol.rds")%>%
  mutate(study = "Study 1")

comb <- s1 %>% rbind(., s2)

library(ggplot2)
comb %>%
  mutate(treatment = factor(treatment, levels=c("Anger", "Worry", "Pooled", "Placebo")))%>%
  filter(treatment!="Placebo")%>%
  ggplot(data=., aes(x=dv, y=Beta_Estimate, color=treatment, shape=treatment)) +
  geom_point(position = position_dodge(width = 0.9))+
  geom_linerange(aes(ymax=CI_95_UL, ymin=CI_95_LL), position = position_dodge(width = 0.9))+
  geom_hline(yintercept=0)+
  theme_report_forcovar_wlegend()+
  coord_flip() +
  ylab("Marginal effect of electoral loss treatments (vs placebo), 95% CI")+
  xlab("Support for political violence \n") +
  labs(color="Treatment", shape="Treatment")+
  ggtitle("Marginal effect of electoral loss treatments on support for political violence") +
   scale_color_manual(values=c('#810f7c','#43a2ca', '#e66101' )) +
    scale_x_discrete(limits=rev)+
  ylim(-0.05, 0.05)+
  facet_grid(~study)


ggsave("figures/fig8.png",width =24, height = 19, units = "cm", dpi=320)


```

# Figure 7, main text, democratic norms

```{r fig7}


s2<- readRDS("data_for_figures/s2_f9_main_demnorms.rds") %>%
  mutate(study = "Study 2")
s1<- readRDS("data_for_figures/s1_f9_main_demnorms.rds")%>%
  mutate(study = "Study 1")

comb <- s1 %>% rbind(., s2)

library(ggplot2)
comb %>%
  mutate(treatment = factor(treatment, levels=c("Anger", "Worry", "Pooled", "Placebo")))%>%
  filter(treatment!="Placebo")%>%
  ggplot(data=., aes(x=dv, y=Beta_Estimate, color=treatment, shape=treatment)) +
  geom_point(position = position_dodge(width = 0.9))+
  geom_linerange(aes(ymax=CI_95_UL, ymin=CI_95_LL), position = position_dodge(width = 0.9))+
  geom_hline(yintercept=0)+
  theme_report_forcovar_wlegend()+
  coord_flip() +
  ylab("Marginal effect of electoral loss treatments (vs placebo), 95% CI")+
  xlab("Support for democratic norms \n") +
  labs(color="Treatment", shape="Treatment")+
  ggtitle("Marginal effect of electoral loss treatments on support for democratic norms") +
   scale_color_manual(values=c('#810f7c','#43a2ca', '#e66101' )) +
    scale_x_discrete(limits=rev)+
  facet_grid(~study)

ggsave("figures/fig9_demnorms.png",width =24, height = 19, units = "cm", dpi=320)



```

# Appendix joint figures 

## Appendix, Figure A11

```{r figa11}

s1 <- readRDS("data_for_figures/s1_f6_main_emo.rds") %>%
  mutate(study = "Study 1")

s2 <- readRDS("data_for_figures/s2_f6_main_emo.rds") %>%
  mutate(study = "Study 2")


comb <- s1 %>% rbind(., s2)

comb <- comb %>% mutate(
  emofac = factor(dv, levels = c("sad", "resigned",  "enthusiastic", "hopeful",  "proud","indifferent","disgusted","disappointed", "anxious", "scared", "afraid", "worried", "outraged", "aversive", "bitter", "resentful", "hateful",  "angry")))

comb %>%
  mutate(treatment = factor(treatment, levels=c("Anger", "Worry", "Pooled", "Placebo")))%>%
  filter(treatment!="Placebo")%>%
  ggplot(data=., aes(x=emofac, y=Beta_Estimate, color=treatment, shape=treatment)) +
  geom_point(position = position_dodge(width = 0.9))+
  geom_linerange(aes(ymax=CI_95_UL, ymin=CI_95_LL), position = position_dodge(width = 0.9))+
  geom_hline(yintercept=0)+
  theme_report_forcovar_wlegend()+
  coord_flip() +
  ylab("Marginal effect of electoral loss treatments (vs placebo), 95% CI")+
  xlab("") +
  labs(color="Treatment", shape="Treatment")+
  ggtitle("Marginal effect of electoral loss treatments on feelings about the election") +
   scale_color_manual(values=c('#810f7c','#43a2ca', '#e66101' )) +
    #scale_x_discrete(limits=rev)+
  facet_grid(~study)



ggsave("figures/f6_emo_discrete.png",width =24, height = 21, units = "cm", dpi=320)



```

## Appendix, Figure A13

```{r figa13}


s2 <- readRDS("data_for_figures/s2_f5_main_accuracy.rds")%>%
  mutate(study = "Study 2")
s1 <- readRDS("data_for_figures/s1_f5_main_accuracy.rds") %>%
  mutate(study = "Study 1")



comb <- s1 %>% rbind(., s2)

comb <- comb %>% mutate(partyid = case_when(study=="Study 1" ~ w1_partyid,
                                            study=="Study 2" ~ "Democrats"),
                        treatment = case_when(study=="Study 1" ~w2_treatment_cat,
                                            study=="Study 2" ~ treatment))

ggplot(comb, aes(x=partyid, y=estimate, fill=treatment, shape=treatment)) +
  theme_report_forcovar_wlegend() +
  geom_bar(stat="identity", position = position_dodge(width = 0.9), alpha=0.65, width=0.7) +
  geom_point(size = 1.5, position = position_dodge(width = 0.9)) +
  geom_linerange( aes(ymin=conf.low, ymax=conf.high), size = 0.5,colour= "grey50" ,position = position_dodge(width = 0.9))  +
    geom_linerange( aes(ymin=conf.low.83, ymax=conf.high.83), size = 1, position = position_dodge(width = 0.9)) +
  theme(
plot.title = element_text(hjust = 0.5, size = 12, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_blank(),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 12),
strip.text.x = element_text(size = 12),
) +
    scale_fill_manual(values=c('#636363','#810f7c','#43a2ca', '#636363','#810f7c','#43a2ca'))+
    labs(x = NULL, y = "Who won the election? % correct answers", title= "Accuracy of election outcome perceptions", fill="Treatment", shape="Treatment", caption = "95% CI (grey)\n83% CI (black)")+ 
  scale_y_continuous(labels = scales::percent)+
  facet_grid(~study, scales = "free_x")


ggsave("figures/f5_accuracy.png",width =24, height = 15, units = "cm", dpi=320)

```

## Appendix, Figure A22

```{r figa22}

library(ggplot2)

s1 <- readRDS("data_for_figures/s1_f7_main_ap.rds") %>%
  mutate(study = "Study 1")

s2 <- readRDS("data_for_figures/s2_f7_main_ap.rds") %>%
  mutate(study = "Study 2")


comb <- s1 %>%
  rbind(s2) %>%
  mutate(
    dv = case_when(
      str_detect(Model, "ap_party") ~ "AP party",
      str_detect(Model, "ap_voters") ~ "AP voters",
      str_detect(Model, "ap_outgroup_party|therm_poloutparty") ~ "Therm (Out-party)",
      str_detect(Model, "ap_ingroup_party|therm_polinparty") ~ "Therm (In-party)",
      str_detect(Model, "ap_outgroup_voters|therm_votersoutparty") ~ "Therm (Out-voters)",
      str_detect(Model, "ap_ingroup_voters|therm_votersinparty") ~ "Therm (In-voters)",
      TRUE ~ "Other"
    )
  )



comb %>%
  mutate(treatment = factor(treatment, levels=c("Anger", "Worry", "Pooled", "Placebo")))%>%
  filter(treatment!="Placebo")%>%
  filter(dv!="NA") %>%
  ggplot(data=., aes(x=dv, y=Beta_Estimate, color=treatment, shape=treatment)) +
  geom_point(position = position_dodge(width = 0.9))+
  geom_linerange(aes(ymax=CI_95_UL, ymin=CI_95_LL), position = position_dodge(width = 0.9))+
  geom_hline(yintercept=0)+
  theme_report_forcovar_wlegend()+
  coord_flip() +
  ylab("Marginal effect of electoral loss treatments (vs placebo), 95% CI")+
  xlab(" \n") +
  labs(color="Treatment", shape="Treatment")+
  ggtitle("Marginal effect of electoral loss treatments on affective polarization") +
   scale_color_manual(values=c('#810f7c','#43a2ca', '#e66101' )) +
    #scale_x_discrete(limits=rev)+
  facet_grid(~study)

ggsave("figures/si_ap.png",width =24, height = 19, units = "cm", dpi=320)


```
